﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MaLu.Data;
using MaLu.Common;
using MaLu.BusiLogic;
using System.Text;
using MaLu.Framework.Common;

namespace MaluJg.web.PovertyIn
{
    public partial class UDeformity : SUserControl
    {
        public void SaveData()
        {
            float f = 0.0f;
            float temp = 0.0f;
            float.TryParse(tbDistrict.Text, out temp);
            f += temp;
            temp = 0.0f;
            float.TryParse(tbTown.Text, out temp);
            f += temp;
            temp = 0.0f;
            float.TryParse(tbMonthMoney.Text, out temp);
            f += temp;

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("declare @id int; set @id={0}; select @id=id from SDeformity where id=@id;", RecordID);
            sb.AppendLine("if @@rowcount =0 begin");
            sb.AppendFormat(@"INSERT INTO [SDeformity]
           ([Name],[Gender],[DeformityType],[DeformityNum],[Address]
           ,[District],[Town],[MonthMoney],[Account],[PeopleCount]
           ,[Remark],[BelongTo],[Type],[Year],[Month],[Money])
     VALUES
           ({0},'{1}',{2},{3},{4},{5},{6},{7},{8},{9},{10},'{11}','{12}',{13},'{14}','{15}'); end else begin

UPDATE [SDeformity]
   SET [Name] ={0}     ,[Gender] = '{1}',[DeformityType]={2},[DeformityNum]={3}
      ,[Address] = {4}     ,[District] =  {5}
      ,[Town] =  {6} ,[MonthMoney]={7},[Account] = {8}
      ,[PeopleCount] =  {9},[Remark] = {10}
      ,[BelongTo] =  '{11}',[Type] = '{12}',[Year] = {13}, [Month]='{14}'   
      ,[Money] =  '{15}'
 WHERE id=@id ; end", Util.ReturnSqlValue(tbName.Text), ddlGendar.SelectedValue, Util.ReturnSqlValue(tbDeformityType.Text), Util.ReturnSqlValue(tbDeformityNum.Text),
                    Util.ReturnSqlValue(tbAddress.Text),  Util.ReturnSqlValue(tbDistrict.Text), Util.ReturnSqlValue(tbTown.Text),
                  Util.ReturnSqlValue(tbMonthMoney.Text), Util.ReturnSqlValue(tbAcount.Text), Util.ReturnSqlValue(tbPeopleCount.Text), Util.ReturnSqlValue(tbRemark.Text), ddlVillage.SelectedItem.Text,
                    ddlType.SelectedValue, Util.ReturnSqlValue(tbYear.Text),ddlMonth.SelectedValue, f );

            SqlHelper.Instance.ExecuteNonQuery(Provider, sb.ToString());

        }
        public void LoadData()
        {
            ddlVillage.DataSource = BLPoverty.GetFType(Provider);
            ddlVillage.DataTextField = "name";
            ddlVillage.DataValueField = "id";
            ddlVillage.DataBind();

            ddlMonth.DataSource = Util.GetMonth();
            ddlMonth.DataTextField = "text";
            ddlMonth.DataValueField = "value";
            ddlMonth.DataBind();
            DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, @"SELECT *  FROM [SDeformity] where id=" + RecordID);
            if (dt.Rows.Count > 0)
            {
                LinkButton lbtnSave = Page.Master.FindControl("ToolBarContentPlaceHolder").FindControl("lbtnSave") as LinkButton;

                lbtnSave.Text = "<span ><img src=\"../Images/Icon/Action/save.gif\" alt=\"保存\" /></span><span>保存</span>";
                Page.Title = "扶贫帮困资金编辑";
                DataRow dr = dt.Rows[0];
                tbYear.Text = dr["Year"].ToString();
                tbName.Text = dr["Name"].ToString();
                tbAcount.Text = dr["account"].ToString();
                tbAddress.Text = dr["address"].ToString();
                tbDistrict.Text = dr["district"].ToString();
                tbTown.Text = dr["town"].ToString();
                tbMonthMoney.Text = dr["MonthMoney"].ToString();
                tbPeopleCount.Text = dr["PeopleCount"].ToString();
                tbRemark.Text = dr["remark"].ToString();
                tbDeformityNum.Text = dr["DeformityNum"].ToString();
                tbDeformityType.Text = dr["DeformityType"].ToString();
                ListItem li = ddlVillage.Items.FindByText(dr["BelongTo"].ToString());
                if (li != null)
                    ddlVillage.SelectedValue = li.Value;
               
                li = ddlType.Items.FindByText(dr["Type"].ToString());
                if (li != null)
                    ddlType.SelectedValue = li.Value;
                li = ddlGendar.Items.FindByText(dr["gender"].ToString());
                if (li != null)
                    ddlGendar.SelectedValue = li.Value;
                li = ddlMonth.Items.FindByText(dr["month"].ToString());
                if (li != null)
                    ddlMonth.SelectedValue = li.Value;

            }
        }
    }
}